<template>
  <div class="container">
    <div class="app-container">
      <div class="main">
        <div class="left">
          <div class="search">
            <el-input
              v-model="queryParams.keyword"
              style="margin-bottom:10px"
              placeholder="输入员工姓名全员搜索"
              size="small"
              prefix-icon="el-icon-search"
              @input="inputValue"
            />
            <!-- 员工左侧树 -->
            <el-tree
              ref="heightTree"
              :data="list"
              :props="defaultProps"
              default-expand-all
              :expand-on-click-node="false"
              highlight-current
              node-key="id"
              @current-change="selectNode"
            />

          </div>
        </div>
        <div class="right">
          <el-row type="flex" justify="end" style="margin:10px">
            <el-button size="mini" type="primary" @click="$router.push('/detail')">添加员工</el-button>
            <el-button size="mini" @click="showExcelDialog=true">excel导入</el-button>
            <el-button size="mini" @click="exportExcel">excel导出</el-button>
          </el-row>
          <!-- 表格 -->
          <el-table :data="employeeList">
            <el-table-column label="头像" prop="staffPhoto" align="center">
              <template v-slot="{row}">
                <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" :size="30" />
                <span v-else class="username">{{ row.username?.charAt(0) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="姓名" prop="username" />
            <el-table-column label="手机号" sortable prop="mobile" />
            <el-table-column label="工号" sortable prop="workNumber" />
            <el-table-column label="聘用形式" prop="formOfEmployment">
              <template v-slot="{row}">
                <span v-if="row.formOfEmployment===1">正式</span>
                <span v-else-if="row.formOfEmployment===2">非正式</span>
                <span v-else>无</span>
              </template>
            </el-table-column>
            <el-table-column label="部门" prop="departmentName" />
            <el-table-column label="入职时间" sortable prop="timeOfEntry" />
            <el-table-column label="操作">
              <template v-slot="{row}">
                <el-button type="text" size="mini" @click="$router.push(`/detail/${row.id}`)">查看</el-button>
                <el-button type="text" size="mini" @click="btnUser(row.id)">角色</el-button>
                <el-button type="text" size="mini" @click="del(row.id)">删除</el-button>
              </template>

            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-row type="flex" justify="end">
            <el-pagination
              layout="total,prev, pager, next"
              :total="total"
              :page-size="queryParams.pagesize"
              :current-page="queryParams.page"
              @current-change="currentChange"
            />
          </el-row>

        </div>
      </div>
    </div>

    <importExcel :showdialog.sync="showExcelDialog" @change="changeSuccess" />
    <el-dialog :visible.sync="showPutDialog" title="分配角色">
      <el-checkbox-group v-model="roleIds">
        <el-checkbox v-for="item in enableList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
      </el-checkbox-group>
      <el-row type="flex" justify="center" style="margin-top: 40px;">
        <el-button type="primary" size="mini" @click="putOK">确定</el-button>
        <el-button size="mini" @click="showPutDialog=false">取消</el-button>
      </el-row>

    </el-dialog>
  </div>
</template>
<script>
import { getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils'
import { employeeList, exportExcel, delUser, getInfo, getEnableRoleList, putUserList } from '@/api/employee'

import FileSaver from 'file-saver'
import importExcel from './component/import-excel.vue'
export default {
  name: 'Employee',
  components: {
    importExcel
  },
  data() {
    return {
      list: [], // 左侧树列表
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 高亮节点需要
      queryParams: {
        departmentId: null,
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      total: 0,
      // 员工列表
      employeeList: [],
      showExcelDialog: false,
      showPutDialog: false, // 空值分配角色弹出框
      enableList: [], // 存储分配权限的列表
      roleIds: [], // 存储id-双向绑定
      currentId: ''
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      this.list = transListToTreeData(await getDepartment(), 0)
      this.queryParams.departmentId = this.list[0].id
      this.$nextTick(() => {
        this.$refs.heightTree.setCurrentKey(this.queryParams.departmentId)
      })
      this.employee()
    },
    async employee() {
      const { rows, total } = await employeeList(this.queryParams)
      this.employeeList = rows
      this.total = total
    },
    selectNode(value) {
      this.queryParams.departmentId = value.id
      this.queryParams.page = 1
      this.employee()
    },
    currentChange(value) {
      this.queryParams.page = value
      this.employee()
    },
    // 模糊搜索
    inputValue() {
      clearTimeout(this.timer)
      this.timer = setTimeout(async() => {
        this.queryParams.pagg = 1
        const res = await this.getDepartment()
        console.log(res)
      }, 500)
    },
    async exportExcel() {
      const res = await exportExcel()
      FileSaver.saveAs(res, '员工信息表.xlsx')
    },
    changeSuccess() {
      this.$message.success('上传成功')
      this.employee()
    },
    async del(id) {
      this.$confirm('你确定要删除吗').then(async() => {
        await delUser(id)
        if (this.employeeList === 1 && this.queryParams.page > 1) this.queryParams.page--
        this.$message.success('删除成功')
        this.employee()
      })
    },
    // 角色按钮
    async btnUser(id) {
      this.currentId = id
      this.enableList = await getEnableRoleList()
      const { roleIds } = await getInfo(id)
      this.roleIds = roleIds
      this.showPutDialog = true
    },
    async putOK() {
      console.log(this.roleIds)
      await putUserList({ id: this.currentId, roleIds: this.roleIds })
      this.$message.success('角色分配成功')
      this.showPutDialog = false
    }
  }
}
</script>
<style scoped lang="scss">
.main{
  background: #fff;
  display: flex;
  justify-content: space-between;
  .left{
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right{
    padding: 20px;
    flex: 1;
  }
  .username{
    display: inline-block;
    line-height: 30px;
    text-align: center;
    width: 30px;
    height: 30px;
    background-color: #04c9be;
    border-radius: 50%;
  }
}
</style>
